﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class frmMttoTipoOfrenda : Form
    {
        [Dependency]
        public ITipoOfrendaBL tipoOfrendaBL { get; set; }

        public frmMttoTipoOfrenda()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool bandera;
        public TIPO_OFRENDA CurrentTipoOfrenda { get; set; }

        public void Nuevo()
        {
            bandera = true;
            tIPO_OFRENDABindingSource.AddNew();
        }

        public void Editar()
        {
            bandera = false;
            tIPO_OFRENDABindingSource.DataSource = CurrentTipoOfrenda;
        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            TIPO_OFRENDA currentTipoOfrenda= tIPO_OFRENDABindingSource.Current as TIPO_OFRENDA;
            var msg = "ERROR";
            var DESCRIPCION = descripcionTextBox.Text.ToUpper();

            if (currentTipoOfrenda!= null)
            {
                if (bandera == true)
                {
                    try
                    {
                        currentTipoOfrenda.Descripcion = DESCRIPCION;
                        tipoOfrendaBL.AddTipoOfrenda(currentTipoOfrenda);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                if (bandera == false)
                {
                    try
                    {
                        currentTipoOfrenda.Descripcion = DESCRIPCION;
                        tipoOfrendaBL.UpdateTipoOfrenda(currentTipoOfrenda);
                        MessageBox.Show("Modificación Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
        }  

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) 
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Space))
            {
                e.Handled = true;
                return;
            }
        }

    }
}
